import { Component, OnInit } from '@angular/core';
import { PromoteService } from '../promote.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { LocalStorageService } from '../../../lib/service/local-storage.service';
import * as moment from 'moment';
import {Router} from "@angular/router";


@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
  providers: [PromoteService]
})
export class ListComponent implements OnInit {

  selectedIndex = 0;
  search_type = 2;
  selectedLabel = '任务名称';
  keyword: string;
  listData = [];
  loading = false;
  page = 1;
  pageSize = 10;
  total;
  flag = true;

  status = null; // 推客状态  0: 正常 1: 自己关闭 2: 管理员关闭
  applyStatus = false;

  constructor(private promoteService: PromoteService,
              private confirmServ: NzModalService,
              private localStorageService: LocalStorageService,
              private _message: NzMessageService,
              private router: Router
              ) { }

  ngOnInit() {
    this.judgeTuikeStatus();
  }

  // 判断商户的推客状态
  judgeTuikeStatus() {
    this.promoteService.getAppletInfo().subscribe(res => {
      this.localStorageService.setNowApplet(res.data);
      const applet = this.localStorageService.getNowApplet();
      this.status = null;
      let tuikeExist = false;
      if (!applet.applet_extend.length) {
        this.status = 1;
      } else {
        applet.applet_extend.every((val, idx, array) => {
          if (val.type === 2) {
            tuikeExist = true;
            if (val.status === 1) {
              this.status = 0;
              this.getListData();
              this.promoteService.getAccountInfo().subscribe(rs => {
                if (rs.code === 2000 ) {
                  if (rs.data.status === 0) {
                    this.status = 0;
                    this.getListData();
                  } else {
                    this.status = 2;
                    this.getListData();
                  }
                } else {
                  this.status = 2;
                  this.getListData();
                  this._message.create('error', rs.message);
                }
              });
            } else {
              this.status = 1;
            }
            return false;
          }
          if (!tuikeExist) {
            this.status = 1;
          }
          return true;
        });
      }
    });
  }


  goAddTask() {
    if(this.flag) {
      this.router.navigate(['member/promote/add']);
      this.flag = false;
    }
  }

  getListData() {
    this.loading = true;
    this.promoteService.getListData(
      this.keyword,
      this.search_type,
      this.selectedIndex,
      this.page,
      this.pageSize).subscribe(res => {
      this.loading = false;
      if (res.code === 2000 ) {
        this.total = res.data.total;
        this.listData = res.data.data;
        if (!!this.listData.length) {
          this.listData.every((val, idx, array) => {
            const start_date = Date.parse(new Date(val.validity_start) + '');
            const end_date = Date.parse(new Date(val.validity_end) + '');
            const now_date = Date.parse(new Date() + '');
            if (end_date < now_date) {
              val['progress'] = '已结束';
            } else if (start_date > now_date) {
              val['progress'] = '未开始';
            } else {
              val['progress'] = '进行中';
            }
            val['expend'] = val.task_data_statistics.task_completed_total * val.point;
            return true;
          });
        }
      } else {
        this._message.create('error', res.message);
      }
    });
  }

  selectChange() {
    switch (this.search_type) {
      case 1:
        this.selectedLabel = '任务名称';
        break;
      case 2:
        this.selectedLabel = '任务ID';
        break;
      case 3:
        this.selectedLabel = '活动名称';
        break;
      case 4:
        this.selectedLabel = '商品名称';
        break;
    }
  }

  changeStatus(id, task_no, status) {
    if (this.status !== 0 ) return;
    let title;
    const _this = this;
    if (status === 1) {
       title = '您是否确定要暂停任务';
    } else  {
      title = '您是否确定要开启任务';
    }
    this.confirmServ.confirm({
      title  : title,
      content: '',
      onOk() {
        _this.promoteService.changeStatus(id, task_no, status).subscribe(res => {
          if (res.code === 2000) {
              _this._message.create('success', '任务状态修改成功');
            _this.getListData();
          } else {
            _this._message.create('error', res.message);
          }
        });
      },
      onCancel() {
      }
    });
  }

  // 申请开通推客
  applytuike() {
    if(!this.applyStatus) {
      this.applyStatus = true;
      this.promoteService.applyTuike().subscribe(res => {
        if (res.code === 2000) {
          this.applyStatus = false;
          this.judgeTuikeStatus();
        } else {
          this.applyStatus = false;
          this._message.create('error', res.message);
        }
      });

    }
  }
}
